package org.smart4j.framework.util;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * 操作类的工具类
 */
public final class ClassUtil {
	private static Logger LOGGER=LoggerFactory.getLogger(ClassUtil.class);
	
	/*
	 * 获取类加载器
	 */
	public static ClassLoader getClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}
	/*
	 * 加载类
	 */
	public static Class<?> loadClass(String className,boolean isInitialized){
		Class<?> cls;
		try {
			cls=Class.forName(className,isInitialized,getClassLoader());
		}catch(ClassNotFoundException e) {
			LOGGER.error("load class failure",e);
			throw new RuntimeException(e);
		}
		return cls;
	}
	/* 
	 * 获取指定包下的所有类
	 */
	public static Set<Class<?>> getClassSet(String packageName){
		Set<Class<?>> classSet= new HashSet<Class<?>>();
		try {
			Enumeration<URL> urls=getClassLoader().getResources(packageName.replace(".", "/"));
			while(urls.hasMoreElements()) {
				URL url=urls.nextElement();
				if(url !=null) {
					String protocol=url.getProtocol();
					if(protocol.equals("file")) {
						String packagePath=url.getPath().replaceAll("%20", " ");
						addClass(classSet,packagePath,packageName);
					}else if(protocol.equals("jar")) {
						
					}
				}
			}
		}catch(Exception e) {
			LOGGER.error("get class set failure",e);
			throw new RuntimeException(e);
		}
		return classSet;
	}
	private static void addClass(Set<Class<?>> classSet, String packagePath, String packageName) {

		
	}
	
}
